package com.situ.ymall.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.ymall.admin.entity.User;
import com.situ.ymall.admin.mapper.UserMapper;
import com.situ.ymall.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo getAll(Integer page, Integer limit, User user) {
        if (page == null) page = 1;
        if (limit == null) {
            List list = userMapper.selectAll();
            limit = list.size();
        }
        PageHelper.startPage(page,limit);
        List list = userMapper.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public User remove(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        user.setStatus(1);
        userMapper.updateByPrimaryKey(user);
        return user;
    }

    @Override
    public User recover(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        user.setStatus(0);
        userMapper.updateByPrimaryKey(user);
        return user;
    }

    @Override
    public User update(User user) {
        userMapper.updateByPrimaryKey(user);
        User selectuser = userMapper.selectByPrimaryKey(user.getId());
        return selectuser;
    }
}
